﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;

namespace Browser
{
    class ParametersManager
    {
        private static string _name;
        private static string _loginUrl;
        private static string _indexUrl;
        private static string _paraFileFullPath;

        public static bool IsRememberUser => string.Equals(GetValue("rem"), "1", StringComparison.OrdinalIgnoreCase);

        public static string UserName => StringEncrypt.DecryptDES(GetValue("user1"), ConstDefine.Password);

        public static string Password => StringEncrypt.DecryptDES(GetValue("user2"), ConstDefine.Password);

        public static string ParametersFileFullPath
        {
            get
            {
                if (string.IsNullOrEmpty(_paraFileFullPath))
                {
                    _paraFileFullPath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath,"Browser.exe.config");
                    if (!System.IO.File.Exists(_paraFileFullPath))
                    {
                        throw new Exception($"配置文件丢失:{_paraFileFullPath}");
                    }
                }
                return _paraFileFullPath;
            }
        }
        /// <summary>
        /// 系统名称，可用于在标题栏显示或在提示信息中显示
        /// </summary>
        public static string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_name))
                {
                    Assembly a = Assembly.GetExecutingAssembly();
                    object[] attrib = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                    _name = ((AssemblyTitleAttribute)attrib[0]).Title;
                }
                return _name;
            }
        }
        /// <summary>
        /// 验证用户是否已经绑定地址
        /// </summary>
        public static string LoginUrl
        {
            get
            {
                if (string.IsNullOrEmpty(_loginUrl))
                {
                    _loginUrl = GetValue("url1");
                    _loginUrl = StringEncrypt.DecryptDES(_loginUrl, ConstDefine.Password);
                }
                return _loginUrl;
            }
        }
        /// <summary>
        /// 系统首页地址
        /// </summary>
        public static string IndexUrl
        {
            get
            {
                if (string.IsNullOrEmpty(_indexUrl))
                {
                    _indexUrl = GetValue("url2");
                    _indexUrl = StringEncrypt.DecryptDES(_indexUrl, ConstDefine.Password);
                }
                return _indexUrl;
            }
        }
        /// <summary>
        /// 将用户信息加密后存储在本地
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public static void RememberUser(string userName, string password)
        {
            //加密后存储
            SetValue("user1", StringEncrypt.EncryptDES(userName,ConstDefine.Password));
            SetValue("user2", StringEncrypt.EncryptDES(password, ConstDefine.Password));
            SetValue("rem", "1");
        }

        public static void ForgetUser()
        {
            SetValue("rem","0");
        }

        private static string GetValue(string key)
        {
            var xml = XElement.Load(ParametersFileFullPath);
            var item = (from e in xml.Descendants("add")
                        where e.Attribute("key")?.Value == key
                        select e).ToList();

            if (item.Count != 1)
            {
                throw new Exception($"配置文件中存在{item.Count}个{key}元素，读取失败");
            }
            return item[0].Attribute("value")?.Value;
        }
        private static void SetValue(string key, string value)
        {
            var xml = XElement.Load(ParametersFileFullPath);
            var item = (from e in xml.Descendants("add")
                where e.Attribute("key")?.Value == key select e).ToList();

            if (item.Count != 1)
            {
                throw new Exception($"配置文件中存在{item.Count}个{key}元素，保存失败");
            }

            item.First().SetAttributeValue("value", value);

            xml.Save(ParametersFileFullPath);
        }
    }
}
